package com.thinkit.bigdata.web.controller.tag;

import com.thinkit.bigdata.core.util.dataSource.DynamicDataSource;
import com.thinkit.bigdata.core.util.es.ESUtil;
import com.thinkit.bigdata.web.coremet.SystemStatic;
import com.thinkit.bigdata.web.model.es.audioEs.Source;
import com.thinkit.bigdata.web.model.perCenter.PendingTaskModel;
import com.thinkit.bigdata.web.model.result.HtmlResult;
import com.thinkit.bigdata.web.service.tag.TagManagerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 语音标注相关业务控制层
 *
 * @author huzenghui
 * @date 2019/10/10
 */
@Controller
@RequestMapping("tagManager")
public class TagManagerController {

    private static final Logger LOGGER = LoggerFactory.getLogger(TagManagerController.class);

    @Resource
    private TagManagerService tagManagerService;

    @Resource
    private ESUtil esUtil;

    /**
     * 标注语音唯一性检查
     *
     * @param serialNumber 语音流水号
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/tagAudioUniqueCheck", method = RequestMethod.POST)
    public HtmlResult tagAudioUniqueCheck(@RequestParam(value = "serialNumber", required = false) String serialNumber) {
        String logTxt = "【标注语音唯一性校验方法】";
        if (serialNumber == null || "".equals(serialNumber)) {
            LOGGER.info("参数serialNumber为空");
            return HtmlResult.failure("参数为空");
        }
        LOGGER.info("{}serialNumber = {}", logTxt, serialNumber);

        // 切换数据源
        DynamicDataSource.setCustomerType(DynamicDataSource.DATASOURCE_TAG);

        // 语音是否已存在标识
        int count = tagManagerService.coutBySerialNumber(serialNumber);

        // 切回默认数据源
        DynamicDataSource.clearCustomerType();

        if (count == 0) {
            LOGGER.info("不存在，唯一性检查通过");
            return HtmlResult.success("语音未标记为转写需标注");
        } else {
            LOGGER.info("已存在，无需再次提交");
            return HtmlResult.failure("语音已标记为转写需标注");
        }
    }

    /**
     * 标注语音添加
     *
     * @param serialNumber 语音流水号
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addTagAudio", method = RequestMethod.POST)
    public HtmlResult tagAudioAdd(@RequestParam(value = "serialNumber", required = false) String serialNumber) {
        String logTxt = "【标注语音添加方法】";
        if (serialNumber == null || "".equals(serialNumber)) {
            LOGGER.info("参数serialNumber为空");
            return HtmlResult.failure("参数为空");
        }
        LOGGER.info("{}serialNumber = {}", logTxt, serialNumber);

        // 根据serialNumber从ES库中获取语音信息
        Source source = esUtil.getSourceBySerialNumber(serialNumber);

        // 切换数据源
        DynamicDataSource.setCustomerType(DynamicDataSource.DATASOURCE_TAG);

        HtmlResult htmlResult = tagManagerService.addTagAudio(source);

        // 切回默认数据源
        DynamicDataSource.clearCustomerType();

        return htmlResult;
    }

    /**
     * 标注语音批量添加
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addBatchTagAudio", method = RequestMethod.POST)
    public HtmlResult tagAudioBatchAdd(HttpServletRequest request) {
        String logTxt = "【标注语音批量添加方法】";
        LOGGER.info(logTxt);
        String pageNum = request.getParameter("pageNum");//起始页
        String pageSize = request.getParameter("pageSize");//每页条数
        String sortName = request.getParameter("sortName");//录音流水
        String sortOrder = request.getParameter("sortOrder");//录音流水
        LOGGER.info(logTxt + "pageNum = " + pageNum);
        LOGGER.info(logTxt + "pageSize = " + pageSize);
        LOGGER.info(logTxt + "sortName = " + sortName);
        LOGGER.info(logTxt + "sortOrder = " + sortOrder);
        try {
            List<PendingTaskModel> result = tagManagerService.searchData(request);
            // 切换数据源
            DynamicDataSource.setCustomerType(DynamicDataSource.DATASOURCE_TAG);
            Integer j = 0;
            for (int i = 0; i < result.size(); i++) {
                // 根据serialNumber从ES库中获取语音信息
                String serialNumber = result.get(i).getSerialNumber();
                int count = tagManagerService.coutBySerialNumber(serialNumber);
                if (count == 0) {
                    LOGGER.info("不存在，唯一性检查通过");
                    Source source = esUtil.getSourceBySerialNumber(serialNumber);
                    tagManagerService.addTagAudio(source);
                } else {
                    j++;
                    LOGGER.info("已存在，无需再次导入");
                }
            }
            // 切回默认数据源
            DynamicDataSource.clearCustomerType();
            return HtmlResult.success("成功批量添加语音" + (result.size() - j) + "条," + j + "条重复语音未导入");
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.info(logTxt + "获取数据异常");
            return HtmlResult.failure("获取数据异常");
        }
    }

    /**
     * 获取配置文件标注系统地址
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getTagAddress", method = RequestMethod.GET)
    public HtmlResult getTagAddress() {
        String logTxt = "【获取标注系统地址】";
        LOGGER.info(logTxt);
        String url = SystemStatic.TAG_URL;
        return HtmlResult.success(url);

    }
}
